﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SLLocalizeVS
{
    public partial class FormParam : Form
    {
        private ParseXAML.Global global;
        private ParseXAML.Params myParams;

        public FormParam()
        {
            InitializeComponent();
        }

        private void Fill(TextBox textBox, ParseXAML.SerializableDictionary<string, ParseXAML.LocalizableControl> data)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var l in data)
            {
                sb.Append(l.Key);
                sb.Append(";");
                foreach (var l2 in l.Value.Properties)
                {
                    sb.Append(l2);
                    sb.Append(",");
                }
                sb.AppendLine();
            }
            textBox.Text = sb.ToString();
        }

        private ParseXAML.ListProperties Parse(TextBox textBox)
        {
            ParseXAML.ListProperties data = new ParseXAML.ListProperties();
            foreach (var l in textBox.Lines)
            {
                if ( string.IsNullOrEmpty( l ) )
                    continue;
                string[] keys = l.Split(new char[] { ';' });
                if (keys.Length < 2)
                    continue;
                string[] properties = keys[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                List<string> o = new List<string>();
                foreach( var p in properties )
                    o.Add( p );
                ParseXAML.LocalizableControl c = new ParseXAML.LocalizableControl();
                c.Properties = o;
                data.Add(keys[0], c);
            }
            return data;
        }

        public void Init(ParseXAML.Params myParams, ParseXAML.Global global)
        {
            this.myParams = myParams;
            this.global = global;

            if (myParams.Method == ParseXAML.Params.eMethod.Code)
                this.cByCode.Checked = true;
            else
                this.cByBinding.Checked = true;

            this.cGenerateWrapperClassForResource.Checked = myParams.GenerateWrapperClassForResource;
            this.cResourceGenerator.Text = myParams.ResXGenerator;
            this.cUpdateResX.Checked = myParams.UpdateResX;
            this.cIgnoreStringBeginBy.Text = myParams.IgnoreStringBeginBy;
            this.cBindingString.Text = myParams.BindingString;

            Fill(this.cInclude, myParams.IncludeProperties);
            Fill(this.cExclude, myParams.ExcludeProperties);
            this.cTryToConvertElementToContent.Checked = myParams.TryToConvertElementToContent;

            this.cCreateNameAttribute.Checked = global.CreateNameAttribute;
            this.cCreateUidAttribute.Checked = global.CreateUidAttribute;
        }

        private void cOK_Click(object sender, EventArgs e)
        {
            myParams.Method = this.cByCode.Checked ? ParseXAML.Params.eMethod.Code : ParseXAML.Params.eMethod.Binding;

            myParams.GenerateWrapperClassForResource = this.cGenerateWrapperClassForResource.Checked;
            myParams.ResXGenerator = this.cResourceGenerator.Text;

            myParams.IncludeProperties = Parse(this.cInclude);
            myParams.ExcludeProperties = Parse(this.cExclude);
            myParams.TryToConvertElementToContent = cTryToConvertElementToContent.Checked;
            myParams.UpdateResX = cUpdateResX.Checked;
            myParams.IgnoreStringBeginBy = cIgnoreStringBeginBy.Text;
            myParams.BindingString = cBindingString.Text;

            global.CreateNameAttribute = this.cCreateNameAttribute.Checked;
            global.CreateUidAttribute = this.cCreateUidAttribute.Checked;
        }

        private void cGenerateWrapperClassForResource_CheckedChanged(object sender, EventArgs e)
        {
            lblResourceGenerator.Enabled = !this.cGenerateWrapperClassForResource.Checked;
            cResourceGenerator.Enabled = lblResourceGenerator.Enabled;
        }

        private void lblResourceGenerator_Click(object sender, EventArgs e)
        {

        }

        private void FormParam_Load(object sender, EventArgs e)
        {

        }
    }
}
